/*
 * Copyright 2012 Biz-e (info@biz-e.nl)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.biz_e.android.calllog.dao;

import nl.biz_e.android.calllog.model.CallLogItem;
import nl.biz_e.android.calllog.model.StoredLogItem;
import nl.biz_e.android.calllog.util.CallLogPrefs;
import android.util.Log;

/**
 * Class to encapsulate all the business logic
 * 
 * @author Joost A. Bloemsma
 */
public class BussinessLogic {
	private static final String DEBUG_TAG = BussinessLogic.class.getSimpleName();
	
	private BussinessLogic() { /* Empty to make class singleton */ }
	
	/**
	 * Logs a {@link CallLogItem} in a calendar.<br/>
	 * The calendar depends on the settings. (See {@link CallLogPrefs}).
	 * @param item
	 * @return The eventId of the new (or modified) calendar item
	 * @throws DaoException
	 * @throws BussinessException
	 */
	public static long logItemInCalendar(CallLogItem item) throws DaoException, BussinessException {
		long id = CalendarDao.getInstance().storeItemInCalendar(item);
		if (id > 0) {
			StoredLogItem logItem = new StoredLogItem(item.getItemId(), CallLogPrefs.getCalendarId(), id);
			LogDBDao.getInstance().storeItem(logItem);
			if (Log.isLoggable(DEBUG_TAG, Log.DEBUG)) Log.d(DEBUG_TAG, "Item " + logItem.getItemId() + " stored");
		} else {
			throw new BussinessException("No calendar ID returned");
		}
		
		return id;
	}
	
	/**
	 * Checks if a certain {@link CallLogItem} is logged in the calendar
	 * @param item
	 * @return True if a calendar event exists for the item.
	 */
	public static boolean isLoggedItem(CallLogItem item) {
		return LogDBDao.getInstance().isLoggedItem(item);
	}
	
}

